package com.ruoyi.hanyun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hanyun.domain.HyProjects;
import com.ruoyi.hanyun.service.IHyProjectsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目Controller
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/hanyun/projects")
public class HyProjectsController extends BaseController {
    @Autowired
    private IHyProjectsService hyProjectsService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:list')")
    @GetMapping("/list")
    public TableDataInfo list(HyProjects hyProjects) {
        startPage();
        List<HyProjects> list = hyProjectsService.selectHyProjectsList(hyProjects);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HyProjects hyProjects) {
        List<HyProjects> list = hyProjectsService.selectHyProjectsList(hyProjects);
        ExcelUtil<HyProjects> util = new ExcelUtil<HyProjects>(HyProjects.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hyProjectsService.selectHyProjectsById(id));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HyProjects hyProjects) {
        return toAjax(hyProjectsService.insertHyProjects(hyProjects));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HyProjects hyProjects) {
        return toAjax(hyProjectsService.updateHyProjects(hyProjects));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('hanyun:projects:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hyProjectsService.deleteHyProjectsByIds(ids));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<HyProjects> util = new ExcelUtil<HyProjects>(HyProjects.class);
        util.importTemplateExcel(response, "任务数据");
    }

    @Log(title = "导入任务信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('hanyun:projects:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HyProjects> util = new ExcelUtil<HyProjects>(HyProjects.class);
        List<HyProjects> projectsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = hyProjectsService.importProjects(projectsList, operName);
        return success(message);
    }

}
